<?php

namespace Kunfeng\Dify\Model\Datasets;

/**
 * 文档分段模型
 */
class Segment
{
    private $id;
    private $documentId;
    private $content;
    private $wordCount;
    private $position;
    private $indexNodeHash;
    private $indexNodeId;
    private $retrieverResource;
    private $answer;
    private $question;
    private $isHit;
    private $hitCount;
    private $enabled;
    private $disabledAt;
    private $disabledBy;
    private $createdBy;
    private $createdAt;
    private $updatedAt;

    public function __construct(array $data = [])
    {
        $this->id = $data['id'] ?? '';
        $this->documentId = $data['document_id'] ?? '';
        $this->content = $data['content'] ?? '';
        $this->wordCount = $data['word_count'] ?? 0;
        $this->position = $data['position'] ?? 0;
        $this->indexNodeHash = $data['index_node_hash'] ?? '';
        $this->indexNodeId = $data['index_node_id'] ?? '';
        $this->retrieverResource = $data['retriever_resource'] ?? null;
        $this->answer = $data['answer'] ?? '';
        $this->question = $data['question'] ?? '';
        $this->isHit = $data['is_hit'] ?? false;
        $this->hitCount = $data['hit_count'] ?? 0;
        $this->enabled = $data['enabled'] ?? true;
        $this->disabledAt = $data['disabled_at'] ?? null;
        $this->disabledBy = $data['disabled_by'] ?? null;
        $this->createdBy = $data['created_by'] ?? '';
        $this->createdAt = $data['created_at'] ?? '';
        $this->updatedAt = $data['updated_at'] ?? '';
    }

    /**
     * 从数组创建
     */
    public static function fromArray(array $data): self
    {
        return new self($data);
    }

    // Getters
    public function getId(): string
    {
        return $this->id;
    }

    public function getDocumentId(): string
    {
        return $this->documentId;
    }

    public function getContent(): string
    {
        return $this->content;
    }

    public function getWordCount(): int
    {
        return $this->wordCount;
    }

    public function getPosition(): int
    {
        return $this->position;
    }

    public function getIndexNodeHash(): string
    {
        return $this->indexNodeHash;
    }

    public function getIndexNodeId(): string
    {
        return $this->indexNodeId;
    }

    public function getRetrieverResource(): ?array
    {
        return $this->retrieverResource;
    }

    public function getAnswer(): string
    {
        return $this->answer;
    }

    public function getQuestion(): string
    {
        return $this->question;
    }

    public function isHit(): bool
    {
        return $this->isHit;
    }

    public function getHitCount(): int
    {
        return $this->hitCount;
    }

    public function isEnabled(): bool
    {
        return $this->enabled;
    }

    public function getDisabledAt(): ?string
    {
        return $this->disabledAt;
    }

    public function getDisabledBy(): ?string
    {
        return $this->disabledBy;
    }

    public function getCreatedBy(): string
    {
        return $this->createdBy;
    }

    public function getCreatedAt(): string
    {
        return $this->createdAt;
    }

    public function getUpdatedAt(): string
    {
        return $this->updatedAt;
    }

    /**
     * 转换为数组
     */
    public function toArray(): array
    {
        return [
            'id' => $this->id,
            'document_id' => $this->documentId,
            'content' => $this->content,
            'word_count' => $this->wordCount,
            'position' => $this->position,
            'index_node_hash' => $this->indexNodeHash,
            'index_node_id' => $this->indexNodeId,
            'retriever_resource' => $this->retrieverResource,
            'answer' => $this->answer,
            'question' => $this->question,
            'is_hit' => $this->isHit,
            'hit_count' => $this->hitCount,
            'enabled' => $this->enabled,
            'disabled_at' => $this->disabledAt,
            'disabled_by' => $this->disabledBy,
            'created_by' => $this->createdBy,
            'created_at' => $this->createdAt,
            'updated_at' => $this->updatedAt,
        ];
    }
}
